<?php

declare(strict_types=1);

namespace app\shop\controller\admin;

use think\Request;
use think\Response;
use sheep\model\Config as SheepConfig;
use sheep\controller\Backend;

class Config extends Backend
{

    /**
     * 基本配置
     *
     * @return Response
     */
    public function basic(Request $request): Response
    {
        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.basic', false);
        } elseif ('PUT' === $request->method()) {

            $configs = SheepConfig::setConfigs('shop.basic', $request->param());
        }
        return success('操作成功', $configs);
    }


    /**
     * 物流配置
     *
     * @return Response
     */
    public function dispatch(Request $request): Response
    {
        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.dispatch', false);
            $configs['callback'] = request()->domain() . '/shop/api/order/express/push';
        } elseif ('PUT' === $request->method()) {

            $configs = SheepConfig::setConfigs('shop.dispatch', $request->param());
        }
        return success('操作成功', $configs);
    }


    /**
     * 平台状态
     *
     * @return Response
     */
    public function platformStatus(Request $request): Response
    {
        $status = [
            'H5' => SheepConfig::getConfigs('shop.platform.H5.status', false),
            'App' => SheepConfig::getConfigs('shop.platform.App.status', false),
            'WechatMiniProgram' => SheepConfig::getConfigs('shop.platform.WechatMiniProgram.status', false),
            'WechatOfficialAccount' => SheepConfig::getConfigs('shop.platform.WechatOfficialAccount.status', false),
        ];

        return success('操作成功', $status);
    }



    /**
     * 平台配置
     *
     * @return Response
     */
    public function platform(Request $request, $platform): Response
    {
        if (!in_array($platform, ['App', 'H5', 'WechatMiniProgram', 'WechatOfficialAccount'])) {
            return error('平台不支持');
        }

        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.platform.' . $platform, false);
        } elseif ('PUT' === $request->method()) {

            $configs = SheepConfig::setConfigs('shop.platform.' . $platform, $request->param());
        }
        return success('操作成功', $configs);
    }


    public function commission(Request $request): Response
    {

        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.commission', false);
        } elseif ('PUT' === $request->method()) {
            check_env('commission');
            $params = $request->param();

            $configs = SheepConfig::setConfigs('shop.commission', $params);
        }
        return success('操作成功', $configs);
    }



    /**
     * 订单配置
     *
     * @return Response
     */
    public function order(Request $request): Response
    {
        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.order', false);
        } elseif ('PUT' === $request->method()) {

            $configs = SheepConfig::setConfigs('shop.order', $request->param());
        }
        return success('操作成功', $configs);
    }


    /**
     * 商品配置
     *
     * @return Response
     */
    public function goods(Request $request): Response
    {
        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.goods', false);
        } elseif ('PUT' === $request->method()) {

            $configs = SheepConfig::setConfigs('shop.goods', $request->param());
        }
        return success('操作成功', $configs);
    }


    /**
     * 充值提现配置
     *
     * @return Response
     */
    public function rechargeWithdraw(Request $request): Response
    {
        if ('GET' === $request->method()) {

            $configs = SheepConfig::getConfigs('shop.recharge_withdraw', false);
        } elseif ('PUT' === $request->method()) {

            $configs = SheepConfig::setConfigs('shop.recharge_withdraw', $request->param());
        }
        return success('操作成功', $configs);
    }

    public function getPlatformUrl()
    {
        $h5Url = SheepConfig::getConfigField('shop.basic.domain');
        $wechatMpAppid =  SheepConfig::getConfigField('shop.platform.WechatMiniProgram.app_id');

        return success('', [
            'url' => $h5Url,
            'appid' => $wechatMpAppid
        ]);
    }
}
